﻿using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace FolderTag.Core
{
    public static class UtilHelper
    {
        /// <summary>
        /// 是否为分享目录
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool IsShare(string path)
        {
            string protocol = path.Substring(0, path.IndexOf("\\"));
            int tempIndex = path.IndexOf("\\", 2) + 1;
            string tempPath = "";
            if (tempIndex > 3 && tempIndex != path.Length)
                tempPath = path.Substring(tempIndex, path.Length - tempIndex);

            return (string.IsNullOrEmpty(protocol) || "SMB".Equals(protocol.ToUpper())) && string.IsNullOrEmpty(tempPath);
        }

        /// <summary>
        /// 读取图片
        /// </summary>
        /// <param name="imagePath"></param>
        /// <returns></returns>
        public static ImageSource GetImage(string imagePath)
        {
            BitmapImage bi = new BitmapImage();
            using (var fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
            {
                MemoryStream ms = new MemoryStream();
                fs.CopyTo(ms);
                //拷贝之后需要重置内存位置
                ms.Seek(0, SeekOrigin.Begin);
                bi.BeginInit();
                bi.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
                bi.StreamSource = ms;
                bi.EndInit();
            }
            return bi;
        }
    }
}
